package com.happly.happlycloud.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.happly.happlycloud.common.Const;
import com.happly.happlycloud.common.ResponseCode;
import com.happly.happlycloud.common.ServerResponse;
import com.happly.happlycloud.dao.MessageMapper;
import com.happly.happlycloud.dao.TutorInfoMapper;
import com.happly.happlycloud.dao.UserMapper;
import com.happly.happlycloud.pojo.Message;
import com.happly.happlycloud.pojo.TutorInfo;
import com.happly.happlycloud.pojo.User;
import com.happly.happlycloud.service.IMessageService;
import com.happly.happlycloud.util.DateTimeUtil;
import com.happly.happlycloud.vo.MessageDetailVo;
import com.happly.happlycloud.vo.MessageListVo;
import com.happly.happlycloud.vo.TutorInfoListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("iMessageService")
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private TutorInfoMapper tutorInfoMapper;
    @Autowired
    private UserMapper userMapper;

    public ServerResponse<String> deleteMessage(Integer messageId){
        if(messageId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int rowCount = messageMapper.deleteByPrimaryKey(messageId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("留言删除成功！");
        }
        return ServerResponse.createByErrorMessage("留言删除失败！");
    }


    public ServerResponse<MessageDetailVo> manageMessageDetail(Integer messageId){
        if(messageId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if(message == null){
            return ServerResponse.createByErrorMessage("此留言不存在");
        }
        MessageDetailVo messageDetailVo = assembleMessageDetailVo(message);
        return ServerResponse.createBySuccess(messageDetailVo);
    }

    private MessageDetailVo assembleMessageDetailVo(Message message){
        MessageDetailVo messageDetailVo = new MessageDetailVo();
        messageDetailVo.setId(message.getId());
        messageDetailVo.setUser_id(message.getUserId());
        messageDetailVo.setUsername(message.getUsername());
        messageDetailVo.setContent(message.getContent());
        messageDetailVo.setCreateTime(DateTimeUtil.dateToStr(message.getCreateTime()));
        messageDetailVo.setUpdateTime(DateTimeUtil.dateToStr(message.getUpdateTime()));
        return messageDetailVo;
    }


    public ServerResponse<PageInfo> getMessageList(int pageNum, int pageSize){
        //startPage-start
        //填充自己的sql查询逻辑
        //pageHelper-收尾
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList = messageMapper.selectList();
        List<MessageListVo> messageListVoList = Lists.newArrayList();
        for (Message messageItem : messageList){
            MessageListVo messageListVo = assembleMessageListVo(messageItem);
            messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }


    private MessageListVo assembleMessageListVo(Message message){
        MessageListVo messageListVo = new MessageListVo();
        messageListVo.setId(message.getId());
        messageListVo.setUsername(message.getUsername());
        messageListVo.setContent(message.getContent());
        messageListVo.setCreatTime(DateTimeUtil.dateToStr(message.getCreateTime()));
        return messageListVo;
    }

    private TutorInfoListVo assembleTutorMessageListVo(TutorInfo message){
        TutorInfoListVo messageListVo = new TutorInfoListVo();
        messageListVo.setId(message.getId());
        User user= userMapper.selectByPrimaryKey(message.getTutorId());
        messageListVo.setUserName(user.getUsername());
        messageListVo.setApplycause(message.getApplycause());
        messageListVo.setTutorCard(message.getTutorCard());
        messageListVo.setTutorGender(message.getTutorGender());
        messageListVo.setTutorEducation(message.getTutorEducation());
        messageListVo.setStatus(message.getStatus());
        messageListVo.setStatusDesc(Const.ApplyStatusEnum.codeOf(message.getStatus()).getValue());
        messageListVo.setCreateTime(DateTimeUtil.dateToStr(message.getCreateTime()));
        return messageListVo;
    }


    public ServerResponse<PageInfo> searchMessage(String userName, Integer messageId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtils.isNotBlank(userName)){
            userName = new StringBuilder().append("%").append(userName).append("%").toString();
        }
        List<Message> messageList = messageMapper.selectByNameAndMessageId(userName,messageId);
        List<MessageListVo> messageListVoList = Lists.newArrayList();
        for (Message messageItem : messageList){
            MessageListVo messageListVo = assembleMessageListVo(messageItem);
            messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<PageInfo> getTutorMessageList(Integer id, int pageNum, int pageSize) {
        //startPage-start
        //填充自己的sql查询逻辑
        //pageHelper-收尾
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList = messageMapper.selectTutorList(id);
        List<MessageListVo> messageListVoList = Lists.newArrayList();
        for (Message messageItem : messageList){
            MessageListVo messageListVo = assembleMessageListVo(messageItem);
            messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<PageInfo> searchMessageTutor(String userName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtils.isNotBlank(userName)){
            userName = new StringBuilder().append("%").append(userName).append("%").toString();
        }
        List<Message> messageList = messageMapper.selectByName(userName);
        List<MessageListVo> messageListVoList = Lists.newArrayList();
        for (Message messageItem : messageList){
            MessageListVo messageListVo = assembleMessageListVo(messageItem);
           messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse addMessage(Integer tutorId, TutorInfo tutorInfo){
        tutorInfo.setTutorId(tutorId);
        int findTutor = tutorInfoMapper.findTutorById(tutorId);
        TutorInfo tutorInfo1 = tutorInfoMapper.selectByUserId(tutorId);
        if (findTutor > 0  && tutorInfo1.getStatus() == 10){
            return  ServerResponse.createByErrorMessage("审核中，请不要重复申请");
        }
        if (findTutor > 0  && tutorInfo1.getStatus() == 0){
            int result    = tutorInfoMapper.updateByPrimaryKeySelective(tutorInfo);
            int changStatus = tutorInfoMapper.changeReApplyStatus(tutorId);
            return  ServerResponse.createByErrorMessage("审核中，请不要重复申请");
        }
        int result    = tutorInfoMapper.insert(tutorInfo);
        int changStatus = tutorInfoMapper.changeStatus(tutorId);
        if(result > 0 && changStatus > 0){
            return ServerResponse.createBySuccessMessage("申请成功");
        }
        return  ServerResponse.createByErrorMessage("申请失败，请稍后再试");
    }

    @Override
    public ServerResponse<PageInfo> getApplyMessageList(int pageNum, int pageSize) {
        //startPage-start
        //填充自己的sql查询逻辑
        //pageHelper-收尾
        PageHelper.startPage(pageNum,pageSize);
        List<TutorInfo> messageList = tutorInfoMapper.selectList();
        List<TutorInfoListVo> messageListVoList = Lists.newArrayList();
        for (TutorInfo messageItem : messageList){
            TutorInfoListVo messageListVo = assembleTutorMessageListVo(messageItem);
            messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse  sureMessage(Integer id) {
        if(id == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        User user = userMapper.selectByName(id);
        int rowCount = userMapper.ChangeSetRole(user.getId(),2);
        if(rowCount > 0){
            int resule = tutorInfoMapper.insertApplyStatus(user.getId());
            if(resule > 0) {
                return ServerResponse.createBySuccessMessage("已设置成为教员！");
            }else{
                return ServerResponse.createByErrorMessage("设置教员失败！");
            }
        }
        return ServerResponse.createByErrorMessage("设置教员失败！");
    }

    @Override
    public ServerResponse<PageInfo> searchApplyMessage(Integer userId,Integer history, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TutorInfo> messageList = tutorInfoMapper.selectByUserCard(userId,history);
        List<TutorInfoListVo> messageListVoList = Lists.newArrayList();
        for (TutorInfo messageItem : messageList){
            TutorInfoListVo messageListVo = assembleTutorMessageListVo(messageItem);
            messageListVoList.add(messageListVo);
        }
        PageInfo pageResult = new PageInfo(messageList);
        pageResult.setList(messageListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse refuseMessage(Integer id) {
        if(id == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
         int resule = tutorInfoMapper.insertRefuseApplyStatus(id);
         if(resule > 0) {
                return ServerResponse.createBySuccessMessage("已拒绝成为教员！");
          }else{
                return ServerResponse.createByErrorMessage("操作失败！");
          }
    }

}
